<?php
/**
 * Created by PhpStorm.
 * User: eric
 * Date: 2016/9/20
 * Time: 13:28
 */

namespace App\Http\Requests;

use App\Exceptions\ServiceException;
use App\Exceptions\ServiceExceptionConstant;
use App\Http\Repositories\Redis\UserRedis;
use Illuminate\Http\Request;

class UserContext
{

    private $id = 0;

    private $nickname = '';

    private $mobile = '';

    private $email = '';

    private $avatar = '';

    private $openid = '';

    private $roleId = '';

    public function __construct(Request $request)
    {

        if ($request->userId) {
            $userInfo = UserRedis::getRedisInstance()->getNode($request->userId);
            if (!$userInfo) {
                throw new ServiceException(ServiceExceptionConstant::getErrorData('need_login'));
            }
            if ($userInfo['status'] == 0) {
                throw new ServiceException(ServiceExceptionConstant::getErrorData('account_band'));
            }
            $this->setId($userInfo['id']);
            $this->setAvatar($userInfo['avatar']);
            $this->setNickName($userInfo['nickname']);
            $this->setMobile($userInfo['mobile']);
            $this->setEmail($userInfo['email']);
            $this->setOpenId($userInfo['openid']);
            $this->setRoleId($userInfo['role_id']);
        }
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getNickName()
    {
        return $this->nickname;
    }

    /**
     * @param mixed $nickname
     */
    public function setNickName($nickname)
    {
        $this->nickname = $nickname;
    }

    /**
     * @return mixed
     */
    public function getMobile()
    {
        return $this->mobile;
    }

    /**
     * @param mixed $mobile
     */
    public function setMobile($mobile)
    {
        $this->mobile = $mobile;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * @return mixed
     */
    public function getAvatar()
    {
        return $this->avatar;
    }

    /**
     * @param mixed $avatar
     */
    public function setAvatar($avatar)
    {
        $this->avatar = $avatar;
    }

    /**
     * @return mixed
     */
    public function getOpenId()
    {
        return $this->openid;
    }

    /**
     * @param mixed $openid
     */
    public function setOpenId($openid)
    {
        $this->openid = $openid;
    }

    /**
     * @param mixed $role_id
     */
    public function setRoleId($role_id)
    {
        $this->roleId = $role_id;
    }

    /**
     * @return mixed
     */
    public function getRoleId()
    {
        return $this->roleId;
    }

}
